# Contributor: ...
# http://aur.archlinux.org/packages.php?ID=37083

pkgname=xbmc-pvr-testing
pkgver=32871
pkgrel=1
pkgdesc="XBMC Media Center with PVR support (VDR ecc..)"
arch=('i686' 'x86_64')
url="https://xbmc.svn.sourceforge.net/svnroot/xbmc/branches/pvr-testing2/"
license=('GPL' 'LGPL')
depends=('bzip2' 'faac' 'faad2' 'fribidi' 'glew' 'jasper' 'libass' 'libcdio' 'libgl' 'libmad' 
         'libmicrohttpd' 'libmms' 'libmodplug' 'libmpeg2' 'libmysqlclient' 'libsamplerate' 
         'libxinerama' 'libxtst' 'lzo2' 'sdl_image>=1.2.10' 'sdl_mixer' 'smbclient' 'unzip' 'wavpack')
makedepends=('cvs' 'boost' 'cmake' 'gperf' 'nasm' 'python' 'subversion' 'zip')
optdepends=('avahi: to use zerconf features (remote, etc...)' 
            'gdb: for meaningful backtraces in case of trouble - STRONGLY RECOMMENDED' 
            'libssh: support for sshfs'
            'libvdpau: accelerated video playback for nvidia cards' 
            'libva: accelerated video playback for nvidia, ati/amd and some intel cards'
            'lirc: remote controller support' 
            'pulseaudio: pulseaudio support'
            'udisks: automount external drives' 
            'upower: used to trigger suspend functionality' 
            'unrar: access compressed files without unpacking them')
options=('makeflags')
provides=('xbmc')
conflicts=('xbmc' 'xbmc-svn' 'xbmc-trunk' 'xbmc-crystalhd' 'xbmc-pulse')
install=("${pkgname}.install")
source=(FEH.sh
         xbmc-pvr-testing.install
         XBMC_patch_from_Gujs.diff 
        )
md5sums=('c3e2ab79b9965f1a4a048275d5f222c4'
          'f93b5bab828190bb7cfd57827f6bcb58'
          '514357d6816b6d0dc106143907ed1be6'
         )

_svnmod=XBMC
_prefix=/usr

build() {
    #MAKEFLAGS="-j1"

    _svntrunk=https://xbmc.svn.sourceforge.net/svnroot/xbmc/branches/pvr-testing2/

    cd ${srcdir}/
    if [ -d $_svnmod/.svn ]; then
        (cd $_svnmod && svn up -r $pkgver) || return 1
    else
        svn co $_svntrunk --config-dir ./ -r $pkgver $_svnmod || return 1
    fi
    msg "SVN checkout done or server timeout"
    msg "Starting make..."

  if [ -d "$srcdir/$_svnmod-build" ]; then
    rm -r "$srcdir/$_svnmod-build"
  fi

    cp -r "$srcdir/$_svnmod" "$srcdir/$_svnmod-build"
    cd "$srcdir/$_svnmod-build"

    
    # Patch for missing projectM presets
   #patch -p0 -i ../projectM.diff || return 1



# http://trac.xbmc.org/ticket/9414
#patch -p0 -i ../tvheadend_enhancements.patch || return 1

# Remove problematic PVR
#sed -i '/xbmc\/pvrclients\/vdr-streamdev \\/d' ./Makefile.in || return 1
#sed -i '/xbmc\/pvrclients\/vdr-vnsi \\/d' ./Makefile.in || return 1
#sed -i '/$(MAKE) -C xbmc\/pvrclients\/vdr-streamdev/d' ./Makefile.in || return 1
#sed -i '/$(MAKE) -C xbmc\/pvrclients\/vdr-vnsi/d' ./Makefile.in || return 1



    # Archlinux Branding by SVN_REV
    export SVN_REV="$pkgver-ARCH"

    # fix lsb_release dependency
#    sed -i -e 's:/usr/bin/lsb_release -d:cat /etc/arch-release:' xbmc/utils/SystemInfo.cpp || return 1



# SSE2 -> SSE
#msg "SSE2 -> SSE"
#sed -i 's/CXXFLAGS+=-I. -DSQUISH_USE_SSE=2 -msse2/CXXFLAGS+=-I. -DSQUISH_USE_SSE=1 -msse/' ./xbmc/lib/libsquish/Makefile.in

# Gujs patch -> ???
msg "Gujs patch"
patch -p0 -i ../XBMC_patch_from_Gujs.diff || return 1 

# remove "--as-needed" from LDFLAGS
unset LDFLAGS; LDFLAGS="-Wl,--hash-style=gnu"



    # Configure XBMC
    msg "Bootstrapping XBMC"
    ./bootstrap || return 1

    msg "Configuring XBMC" 
     ./configure  --prefix=${_prefix} \
                  --enable-external-libraries \
                  --disable-external-ffmpeg \
                  --disable-external-python \
                  --disable-debug || return 1

  # Now (finally) build
  msg "Running make" 
  make || return 1
  make -C lib/addons/script.module.pil || return 1
  make -C lib/addons/script.module.pysqlite || return 1
}

package() {
  cd "${srcdir}/${_svnmod}-build"

  msg "Running make install" 
  make prefix=${pkgdir}${_prefix} install || return 1

  # Replace FEH.py with FEH.sh (and thus remove external python dependency)
  install -D -m 0755 ${srcdir}/FEH.sh ${pkgdir}${_prefix}/share/xbmc/FEH.sh || return 1
  sed -i -e 's/^python \(.*\)FEH.py \(.*\)$/\1FEH.sh \2/' ${pkgdir}${_prefix}/bin/xbmc || return 1

  # lsb_release fix
#  sed -i -e 's/which lsb_release &> \/dev\/null/\[ -f \/etc\/arch-release ]/g' ${pkgdir}${_prefix}/bin/xbmc || return 1
#  sed -i -e "s/lsb_release -a 2> \/dev\/null | sed -e 's\/\^\/    \/'/cat \/etc\/arch-release/g" ${pkgdir}${_prefix}/bin/xbmc || return 1

  # .desktop files
#  install -D -m 0644 ${srcdir}/${_svnmod}-build/tools/Linux/xbmc.desktop ${pkgdir}${_prefix}/share/applications/xbmc.desktop || return 1
#  install -D -m 0644 ${srcdir}/${_svnmod}-build/tools/Linux/xbmc.png ${pkgdir}${_prefix}/share/pixmaps/xbmc.png || return 1

  # Tools
#  install -D -m 0755 ${srcdir}/${_svnmod}-build/xbmc-xrandr ${pkgdir}${_prefix}/share/xbmc/xbmc-xrandr || return 1
#  install -D -m 0755 ${srcdir}/${_svnmod}-build/tools/TexturePacker/TexturePacker ${pkgdir}${_prefix}/share/xbmc/ || return 1

  # License(s)
  #install -dm755 ${pkgdir}/usr/share/licenses/${pkgname}
  #for licensef in LICENSE.GPL copying.txt; do 
    #mv ${pkgdir}${_prefix}/share/doc/${licensef} ${pkgdir}/usr/share/licenses/${pkgname} || return 1 
  #done

  # Docs 
  #install -d -m 0755 ${pkgdir}${_prefix}/share/doc/${pkgname} 
  #for docsf in keymapping.txt README.linux; do 
    #mv ${pkgdir}${_prefix}/share/doc/${docsf} ${pkgdir}/usr/share/doc/${pkgname} || return 1 
  #done

  # cleanup some stuff
  #msg "Cleanup unneeded files"
  #rm -rf ${pkgdir}/usr/share/xsessions
  #rm -f ${pkgdir}/usr/share/xbmc/FEH.py

  # strip
  #msg "Stripping binaries"
  #find $pkgdir -type f -exec strip {} \; >/dev/null 2>/dev/null

  # use old dir scheme
  #mv ${pkgdir}${_prefix}/lib/xbmc/* ${pkgdir}${_prefix}/share/xbmc/ || return 1
  #rm -rf ${pkgdir}${_prefix}/lib/xbmc



# cp -R ${pkgdir}${_prefix}/share/xbmc/ ${pkgdir}${_prefix}/lib/ || return 1

# Fix icon cache
rm ${pkgdir}${_prefix}/share/icons/hicolor/icon-theme.cache



  # fix LIBDIR in /usr/bin/xbmc
#  sed -e 's/LIBDIR="\/usr\/lib/LIBDIR="\/usr\/share/g' -i ${pkgdir}${_prefix}/bin/xbmc
}
